package com.jxwifi.cloud.quickclean.quicktreasure;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.support.design.widget.TabLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.ClientBasicActivity;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.app.StateUtils;
import com.jxwifi.cloud.quickclean.pay.PayResult;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;
import com.umeng.analytics.MobclickAgent;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QuickTreasureActivity extends ClientBasicActivity implements QuickTreasureView {

    @Bind(R.id.tv_quick_price)
    TextView mTvQuickPrice;
    @Bind(R.id.tl_quick_tabLayout)
    TabLayout mTlQuickTabLayout;
    @Bind(R.id.quick_viewPager)
    ViewPager mQuickViewPager;
    @Bind(R.id.tv_quick_balance)
    TextView mTvQuiclBalance;

    private QuickPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_treasure);
        ButterKnife.bind(this);
        openEventBus();
        initHeadLayout();
        mPresenter=new QuickPresenter(mBaseContext,this);
    }

    /**
     * 初始化浸入式标题
     */
    private void initHeadLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Subscriber(tag = "e-QuickResult")
    private void getquicktion(String SurplusAmount) {
        mTvQuickPrice.setText("\u00A5" + SurplusAmount);
    }

    @OnClick({R.id.rel_img_back_pressed, R.id.tv_quick_recharge,R.id.tv_quick_exchange})
    void clickCK(View v) {
        switch (v.getId()) {
            case R.id.rel_img_back_pressed:
                onBackPressed();
                break;
            //购买套餐
            case R.id.tv_quick_recharge:
                Intent intent=new Intent();
                intent.setClass(mBaseContext,BuySetMealActivity.class);
                startActivity(intent);
                MobclickAgent.onEvent(mBaseContext, "tv_quick_recharge");
                break;
                //兑换套餐
            case R.id.tv_quick_exchange:
                mPresenter.RedemptionPackage();
                break;
        }
    }

    @Override
    public TextView getBalance() {
        return mTvQuiclBalance;
    }

    @Override
    public void oninitData() {
        PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
        if (PasswordLogin.mIsLogin) {
            mPresenter.initData();
        }
    }

    @Override
    public void onFailure(int errorCode, String msg) {
        Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
        ToastUtils.showMyToast(toast, 3000);
    }


    @Override
    public FragmentManager getFrgaments() {
        return getSupportFragmentManager();
    }
    @Override
    public ViewPager getPage() {
        return mQuickViewPager;
    }

    @Override
    public TabLayout getTabLayouts() {
        return mTlQuickTabLayout;
    }

    @Override
    public TextView getQuickPrice() {
        return mTvQuickPrice;
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.initData();
    }
}
